<?php

namespace common\models\cso;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%cso_user}}".
 *
 * @property int $id
 * @property string $name
 * @property string $mobile
 * @property string $avatar 操作类型值
 * @property int $user_id 用户ID
 * @property string $qrcode
 * @property int $type
 * @property string $description
 * @property int $created_at 创建时间
 * @property int $updated_at
 */
class CsoUser extends \common\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\User;

    const TYPE_ONLINE = 1;
    const TYPE_BUSSINESS = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cso_user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'mobile'], 'required'],
            [['user_id', 'type', 'created_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['mobile'], 'string', 'max' => 11],
            [['avatar', 'qrcode', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'mobile' => '手机号',
            'avatar' => '头像',
            'user_id' => '用户ID',
            'qrcode' => '二维码',
            'type' => '类型',
            'description' => '备注',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            //
            'orderNums' => '接单数',
            'userNums' => '用户数',
        ];
    }

    public function getOrders() {
        return $this->hasMany(CsoUserOrder::class, ['cso_id' => 'id']);
    }

    public function getUserNums() {
        return $this->getOrders()->groupBy('user_id')->count();
    }

    public function getOrderNums() {
        return $this->getOrders()->count();
    }

    public static function defaultServer() {
        return static::find()->andWhere(['type' => static::TYPE_ONLINE])->one();
    }

    public static function findOneLink($uid) {
        $servers = static::find()->andWhere(['type' => static::TYPE_ONLINE])->all();
        $nums = count($servers);
        if ($nums < 1) {
            return static::defaultServer();
        }
        $no = $uid % $nums;
        $server = ArrayHelper::getValue($servers, $no);
        if (!$server) {
            return static::defaultServer();
        }
        return $server;
    }

    public static function findDefault($uid = 0) {
        $servers = static::find()->all();
        $content = [];
        $onlines = [];
        $serverUserId = 1;
        foreach ($servers as $server) {
            if ($server->type == static::TYPE_BUSSINESS) {
                $content[] = Url::attach($server->qrcode);
            } elseif ($server->type == static::TYPE_ONLINE) {
                $onlines[] = $server->user_id;
            }
        }
        $nums = count($onlines);
        if ($nums > 0) {
            $no = $uid % $nums;
            $serverUserId = ArrayHelper::getValue($onlines, $no);
        }

        return [
            'user_id' => $serverUserId,
            'name' => '优越定制客服',
            'content' => $content,
        ];
    }

    public function fields() {
        return [
            'id' => 'user_id',
            'name',
            'avatar' => function() {
                return Url::attach($this->avatar);
            },
            'service_id' => 'id',
            'qrcode' => function() {
                return Url::attach($this->qrcode);
            },
        ];
    }

}
